package com.xhh.enjoyapp.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.xhh.enjoyapp.Bean.User;
import com.xhh.enjoyapp.R;
import com.xhh.enjoyapp.activity.Login;
import com.xhh.enjoyapp.activity.MyInfo;
import com.xhh.enjoyapp.activity.MyPushInvitation;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

/**
 * @description
 * @author: weiXhh
 * @create: 2020-05-14 10:34
 **/
public class FragmentMine extends Fragment {

    private TextView username;
    private Button logout;
    private LinearLayout myInfo;
    private LinearLayout myInfoPushInvitation;
    private LinearLayout myInfoPushCommunity;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_mine, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();

        // 加载个人信息
        getMyInfo();

        logout.setOnClickListener(click->{
            BmobUser.logOut();
            startActivity(new Intent(getActivity(), Login.class));
            getActivity().finish();
        });

        // 我的详细信息
        myInfo.setOnClickListener(click->{
            startActivity(new Intent(getActivity(), MyInfo.class));
        });

        // 我的帖子
        myInfoPushInvitation.setOnClickListener(click->{
            startActivity(new Intent(getActivity(), MyPushInvitation.class));
        });
    }

    private void getMyInfo() {
        BmobUser bu = BmobUser.getCurrentUser(BmobUser.class);
        String id = bu.getObjectId();
        BmobQuery<User> bmobQuery = new BmobQuery<>();
        bmobQuery.getObject(id, new QueryListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if(e == null){
                    username.setText(user.getUsername());
                } else {
                    Toast.makeText(getActivity(), "加载失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {
        username = getActivity().findViewById(R.id.mine_fragment_username);
        logout = getActivity().findViewById(R.id.mine_fragment_logout);
        myInfo = getActivity().findViewById(R.id.mine_fragment_info);
        myInfoPushInvitation = getActivity().findViewById(R.id.mine_fragment_invitation);
        myInfoPushCommunity = getActivity().findViewById(R.id.mine_fragment_community);

    }
}
